﻿package com.ui
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    import flash.display.Shape;
    
    /**
     * ...
     * @author sherrie
     */
    public class ImageCut extends Sprite 
    {
        
        private var _url:String;
        private var _loader:Loader;
        public var maintainAspectRatio:Boolean = true;
        public var background:Boolean = false;
        public var backgroundColor:uint;
        
        public function ImageCut(url:String) 
        { 
            this._url = url;
            configLoader();
            
        }
        
        private function configLoader():void {
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            _loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            _loader.load(new URLRequest(url));
            
        }
        
        
        public function setSize(w:Number, h:Number, maintainAspectRatio:Boolean = true):void {
            this.maintainAspectRatio = maintainAspectRatio;
            
            if (this.maintainAspectRatio) { trace('if');
                   //maintain aspect ratio
                var scale_x:Number;
                var scale_y:Number;
                var contentWidth:Number = _loader.content.width;
                var contentHeight:Number = _loader.content.height;
                
                if (contentWidth * h > contentHeight * w) {     
                    _loader.width = w;             
                    scale_x = w/contentWidth;
                    _loader.height = contentHeight * scale_x;     
                
                }else {
                    _loader.height = h;
                    scale_y = h/contentHeight;
                    _loader.width = contentWidth * scale_y;
                }
            }else { trace('here');
                _loader.width = w;
                _loader.height = h;
            }
            addChild(_loader);
            
        }
        
        public function cutSizeByBitmap(w:Number, h:Number):Bitmap {
            var contentWidth:Number = _loader.content.width;
            var contentHeight:Number = _loader.content.height;
            var scale_x:Number;
            var scale_y:Number;
            var mask_x:Number;
            var mask_y:Number;
            var bitmap:Bitmap
            
            if (contentWidth * h > contentHeight * w) { 
                _loader.height = h;
                
                //this.width = w; trace('this.width',this.width);

                scale_y = h/contentHeight;
                _loader.width = contentWidth*scale_y;
                
                mask_x = _loader.width / 2 - w / 2;
                _loader.x = -mask_x;


                trace('_loader.height', _loader.height);
                trace('_loader.width', _loader.width);
                trace('_loader.x', _loader.x);

            }else if(contentWidth*h == contentHeight*w){
                _loader.width = w;
                _loader.height = h;
                
            }else { 
                _loader.width = w;

                //set item to a fixed height
                //this.height = h;

                scale_x = w/contentWidth;
                _loader.height = contentHeight*scale_x;
                
                mask_y = _loader.height/2-h/2;
                _loader.y = -mask_y;
    
            }  
            
            var bmpData:BitmapData = new BitmapData(w, h);
            bmpData.draw(_loader, _loader.transform.matrix, _loader.transform.colorTransform,null,null,true);
                
            bitmap = new Bitmap(bmpData);
            bitmap.smoothing = true;
            
            addChild(bitmap);
            return bitmap;
            
        }
        
        public function cutSize(w:Number, h:Number):void {
            var contentWidth:Number = _loader.content.width;
            var contentHeight:Number = _loader.content.height;
            var scale_x:Number;
            var scale_y:Number;
            var mask_x:Number;
            var mask_y:Number;
            var mask:Shape;
            
            if (contentWidth * h > contentHeight * w) { 
                _loader.height = h;
                
                //this.width = w; trace('this.width',this.width);

                scale_y = h/contentHeight;
                _loader.width = contentWidth*scale_y;
                
                mask_x = _loader.width / 2 - w / 2;
                
                mask = new Shape();
                mask.graphics.beginFill(0x000000,1);
                mask.graphics.drawRect(0,0,w,h);
                mask.graphics.endFill();
                addChild(mask);
                
                _loader.mask = mask;
                
                _loader.x = -mask_x;    
                
                trace('_loader.height', _loader.height);
                trace('_loader.width', _loader.width);
                trace('_loader.x', _loader.x);

            }else if(contentWidth*h == contentHeight*w){
                _loader.width = w;
                _loader.height = h;
                
            }else { 
                _loader.width = w;

                //set item to a fixed height
                //this.height = h;

                scale_x = w/contentWidth;
                _loader.height = contentHeight*scale_x;
                
                mask_y = _loader.height/2-h/2;
                
                mask = new Shape();
                mask.graphics.beginFill(0x000000,1);
                mask.graphics.drawRect(0,0,w,h);
                mask.graphics.endFill();
                addChild(mask);
                
                _loader.mask = mask; 

                _loader.y = -mask_y;
            }  
            addChild(_loader);
            
        }
        
        private function completeHandler(event:Event):void {
            trace('complete');
            dispatchEvent(new Event(Event.COMPLETE));
            
            (_loader.content as Bitmap).smoothing = true;
            
            
        }
        
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace(event.toString());
            dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
        }
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace(event.toString());
            dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
        }
        
        public function set url(_url:String):void {
            this._url = _url
        }
        public function get url():String {
            return this._url;
        }
        
    }
    
}
